﻿using System;
using MetaCollection.Framework.Model;

namespace MetaCollection.Framework.Security
{
	/// <summary>
	/// Associates a set of required AccessRights with an Enumeration value or method.
	/// </summary>
	[global::System.AttributeUsage(AttributeTargets.Method | AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
	public sealed class RequiredAccessRightsAttribute : Attribute
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="AccessRightsAttribute"/> class with the RequiredRights
		/// with ItemTargets.Self
		/// </summary>
		/// <param name="requiredRights">The required rights.</param>
		public RequiredAccessRightsAttribute(AccessRights requiredRights)
		{
			this.RequiredRights = requiredRights;
			this.Targets = ItemTargets.Self;
			this.Message = String.Empty;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="RequiredAccessRightsAttribute"/> class.
		/// </summary>
		/// <param name="requiredRights">The required rights.</param>
		/// <param name="targets">The item targets for these rights.</param>
		public RequiredAccessRightsAttribute(AccessRights requiredRights, ItemTargets targets)
		{
			this.RequiredRights = requiredRights;
			this.Targets = targets;
			this.Message = String.Empty;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="RequiredAccessRightsAttribute"/> class.
		/// </summary>
		/// <param name="requiredRights">The required rights.</param>
		/// <param name="targets">The item targets for these rights.</param>
		/// <param name="message">The message.</param>
		public RequiredAccessRightsAttribute(AccessRights requiredRights, ItemTargets targets, string message)
		{
			this.RequiredRights = requiredRights;
			this.Targets = targets;
			this.Message = message;
		}

		/// <summary>
		/// Rights Required of a user on an item for this Http.Method
		/// </summary>
		public AccessRights RequiredRights { get; private set; }

		/// <summary>
		/// The Items to associate the AccessRights demands with
		/// </summary>
		public ItemTargets Targets { get; private set; }

		/// <summary>
		/// Gets or sets the message associated with this Access Rights Requirement.
		/// </summary>
		/// <value>The message to display if this rights requirement is not satisfied.</value>
		public string Message { get; private set; }
	}

	/// <summary>
	/// Set of targets for the RequiredAccessRightsAttribute
	/// </summary>
	[Flags]
	public enum ItemTargets
	{
		None = 0,
		Parent = 1,
		Self = 1 << 1,
		Children = 1 << 2,
		All = Parent | Self | Children
	}
}
